<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>腾飞健康管理平台</title>
    <link rel="stylesheet" href="lib/layui/css/layui.css">
</head>
<body>
<div style="margin-top: 50px; margin-right: 80px; margin-left: 50px">
    <form class="layui-form">
        <div class="layui-form-item ">
            <label class="layui-form-label">原密码</label>
            <div class="layui-input-block">
                <input type="password" name="originalPwd" placeholder="请输入原密码" class="layui-input"
                       lay-verify="required" autocomplete="off" maxlength="16">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="newPwd" placeholder="请输入新密码" class="layui-input" lay-verify="required"
                       autocomplete="off" maxlength="16">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="confirmPwd" placeholder="请再次输入新密码" class="layui-input"
                       lay-verify="required" autocomplete="off" maxlength="16">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitPwd">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script src="lib/layui/layui.js" type="text/javascript"></script>
    <script type="text/javascript">
        layui.use(["jquery", "form"], function () {
            let $ = layui.jquery;
            let form = layui.form;


            //提交数据
            form.on("submit(submitPwd)", function (data) {
                //对比‘确认密码’中的数据和‘新密码’中的数据是否一样，如果一样，就提交数据，否则不提交。
                if (data.field.newPwd != data.field.confirmPwd) {
                    layer.msg("确认密码和新密码不一致", {icon: 2, time: 1500});
                    return false;
                } else {
                    let loadIndex;
                    $.ajax({
                        url: "/updateSelfPassword",
                        type: "post",
                        dataType: "json",
                        data: data.field,
                        async: true,
                        beforeSend: function () {
                            loadIndex = layer.load();
                        },
                        success: function (responseResult) {
                            layer.close(loadIndex);
                            if (responseResult.code == 1) {
                                layer.open({
                                    type: 1,
                                    title: "提示",
                                    btn: "确定",
                                    btnAlign: "c", //按钮居中
                                    closeBtn: 0,   //去掉右上角关闭按钮
                                    content: "<div style='padding: 20px 80px;'>" + responseResult.msg + '</div>',
                                    yes: function () {
                                        //点击关闭按钮，回到登录页面
                                        $.ajax({
                                            url: "/logout",
                                            type: "post",
                                            async: true,
                                            success: function (responseResult) {
                                                window.location = "/unitLogin";
                                            }
                                        });
                                    }
                                });
                                console.log(responseResult.msg);
                            } else {
                                layer.msg(responseResult.msg, {icon: 2, time: 1500});
                                console.log(responseResult.msg);
                            }
                        },
                        error: function (XMLhttpRequest, textSatus, errorThrown) {
                            layer.close(loadIndex);
                            layer.msg("服务器错误", {icon: 2, time: 1500});
                            console.log("服务器错误");
                        },
                    });
                }
                return false;
            });

        });
    </script>
</div>

</body>
</html>